<?php

namespace App\Excel\Export;

use App\Dao\Order\OrderDao;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\Excel\Concerns\Exportable;
use Yj\Excel\Concerns\FromQuery;
use Yj\Excel\Concerns\WithHeadings;
use Yj\Excel\Concerns\WithMapping;
use Yj\Excel\Concerns\WithProperties;
use function Swoole\Coroutine\Http\request;

/**
 * @ApiController(prefix="export/order")
 */
class OrderExport implements FromQuery,WithHeadings,WithMapping
{
    use Exportable;

    /**
     * @Inject()
     * @var OrderDao
     */
    protected $orderDao;


    /**
     * @Inject()
     * @var RequestInterface
     */
    protected $request;

    public function headings(): array
    {
        return [
            '类型',
            '名称',
            '手机号码',
            '产权年限',
            '业态类型',
            '拍摄类型',
            '是否将系统默认的AI机器人配音升级为真人配音',
            '是否做同步户型图跟踪',
            '是否追加平台广告',
            '是否加急',
            '卖点',
        ];
    }


    public function map($invoice): array
    {
        $maidian = [];
        $invoice->OrderMaidianDesc->each(function ($value)use(&$maidian){
            $maidian[] =$value->maidian->name.':'.trim($value->desc,',');
           return true;
        });

        return  [
            $invoice->user_type_format,
            $invoice->name,
            $invoice->tel,
            $invoice->house_years,
            $invoice->house_type_format,
            $invoice->photo_type_format,
            $invoice->is_real_person_format,
            $invoice->is_sync?'是':'否',
            $invoice->is_ad_format,
            $invoice->is_accelerate?'是':'否',
            implode(';',$maidian)
        ];
    }

    /**
     * @PostApi(path="run")
     */
    public function run(){
//        $this->store($this,time().'xlxs');


        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $line = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

        foreach ($this->headings() as $k => $v) {
            $sheet->setCellValue($line[$k] . '1', $v);
            $sheet->getColumnDimension($line[$k])->setWidth(20);
        }

        foreach ($this->query() as $k=> $item) {
            foreach ($this->map($item) as $kk=>$value){
                $sheet->setCellValue($line[$kk] . ($k + 2), $value);
            }
        }
        $writer = new Xlsx($spreadsheet);
        $path = '/excel/' .  time() . '.xlsx';
        $writer->save(config('file.storage.local.root') .$path);
        $spreadsheet->disconnectWorksheets();
        _SUCCESS(($path));
    }

    public function query()
    {
        $data = $this->orderDao->DaoWhere(['order_id'=>$this->request->post('order_id')])->orderByDesc('id')->where('status','>',0)
            ->with([
                'OrderMaidianDesc'=>function($query){
                    $query->with('Maidian');
                },
                'Users',
                'OrderChangeDetail',
                'vedioPath',
                'vedio1Path'
            ])->get();
        return $data;
    }

}